<template>
  <div class="login">
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">亚诚后台管理系统</h3>
      <el-form-item prop="account">
        <el-input v-model="loginForm.account" type="text" size="large" auto-complete="off" placeholder="账号">
          <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="密码" @keyup.enter="handleLogin">
          <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="captcha" v-if="captchaEnabled">
        <el-input v-model="loginForm.captcha" auto-complete="off" placeholder="验证码" @keyup.enter="handleLogin" class="codeInput">
          <template #append>
            <div class="login-code" v-html="codeInfo.data" @click="getCode"></div>
          </template>
        </el-input>
      </el-form-item>
      <!-- <el-checkbox v-model="loginForm.keeplogin" :true-label="1" :false-label="0" style="margin: 0px 0px 25px 0px">24小时免登录</el-checkbox> -->
      <el-form-item style="width: 100%">
        <el-button :loading="loading" size="large" type="primary" style="width: 100%" @click.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <el-link type="primary" href="https://beian.miit.gov.cn" target="_blank">苏ICP备2024060534号</el-link>
    </div>
  </div>
</template>

<script setup lang="ts">
import Cookies from "js-cookie";
import { setToken } from "@/utils/auth";
import { onMounted } from "vue";
const { proxy } = getCurrentInstance();
//counter
import useCounterStore from "@/store/counter";
const counter = useCounterStore();
//router
import { useRoute } from "vue-router";
const route = useRoute();

onMounted(() => {
  if (route.query.redirect) {
    redirect.value = route.query.redirect;
  }
  if (window.location.hostname == "localhost") {
    loginForm.value.account = "admin";
    loginForm.value.password = "yachengedu1209";
  }
  getCode();
});

let codeInfo = ref({});
const loginForm = ref({
  account: "",
  password: "",
  keeplogin: 0,
  captcha: null,
});
const validateCode = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入验证码"));
  } else if (value != codeInfo.value.text) {
    callback(new Error("请输入正确的验证码"));
  } else {
    callback();
  }
};
const loginRules = {
  account: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  captcha: [{ required: true, trigger: "change", validator: validateCode }],
};

const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);

function handleLogin() {
  proxy.$refs.loginRef.validate((valid) => {
    if (valid) save();
  });
}

let save = async () => {
  try {
    loading.value = true;
    let token = await proxy.$apis.login(loginForm.value);
    setToken(token);
    counter.setYcParams("token", token);
    console.log("ycAdminParams", counter.ycAdminParams);
    await counter.getUserInfo(loginForm.value.account);
    proxy.$utils.showToast("登录成功");
    proxy.$utils.toLink(redirect.value || "/");
  } catch (error) {
    console.log("啥问题", error);
  } finally {
    getCode();
    loading.value = false;
  }
};

async function getCode() {
  codeInfo.value = await proxy.$apis.getCaptcha();
}
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
::v-deep .codeInput {
  .el-input-group__append {
    padding: 0;
    .login-code {
      cursor: pointer;
      width: 100%;
      height: 100%;
      svg {
        width: 100%;
        height: 100%;
      }
    }
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
</style>
